//
//  RoleModel.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import Foundation
import KakaJSON
import SwiftyJSON
import WCDBSwift

enum RoleModelType: Int {
    case bot = 0
    case elon = 1
    case bella = 2
    case jeans = 3
    case mia = 4
    case aria = 5
    case emily = 6
    case kanye = 7
    case ariana = 8
    case im = 9
    case cristiano = 10
    case naruto = 11
    case monkey = 12
    case satoru = 13
    case nezuko = 14
    case yor = 15
    case ganyu = 16
    case mario = 17
    case bb = 18
    case juri = 19
    case link = 20
    case matthew = 21
    case liam = 22
    case miller = 23
    case andrew = 24
    case evan = 25
}

final class RoleModel: ChatUpDBBaseModel, ChatUpDBStorageProtocol {
    var id: Int = 0
    var roleType: Int = RoleModelType.bot.rawValue
    var name: String = ""
    var serverId: Int = 0
    var questionTemplate: String = ""
    var icon: String = ""
    // 角色问候语
    var greeting: String = ""
    var desc: String = ""
    var lastTime: Int64 = 0
    var lastQuestion: String = ""
    // 分类
    var category: String = ""
    var categoryId: Int = 0
    var images:[String] = []
    var imageIndex:Int = 1
    //  var extraType:Int64 = 0

    convenience init(type: Int, questionTemplate: String, icon: String, greeting: String?, desc: String?, lastTime: Int64, name: String, serverId: Int, category: String, images: [String],imageIndex: Int) {
        self.init()
        self.roleType = type
        self.id = serverId
        self.questionTemplate = questionTemplate
        self.icon = icon
        self.greeting = greeting ?? "role"+String(serverId)+"_greetings"
        self.desc = desc ?? "role"+String(serverId)+"_desc"
        self.lastTime = lastTime
        self.name = name
        self.serverId = serverId
        self.category = category
        self.images = images
        self.imageIndex = imageIndex
        self.categoryId = getCategoryId(category: category)
//        self.extraType = extraType
    }

    //  static func getData(_ jsonData: JSON) -> [RoleRespondModel] {
//    let datas: [RoleRespondModel] = jsonData.map { _ , data in
//        return RoleRespondModel().createWithJsonData(data)
//    }
//    return datas
    //  }

    override func tableName() -> String? {
        return "role_db"
    }
    
    func i18nDesc() -> String {
        return String.i18nText(desc)
    }
    
    func i18nGreeting() ->String {
        return serverId == 0 ? greeting : String.i18nText(greeting)
    }
    
    func currentImage()-> String{
        return images[imageIndex % 4] 
    }

    /** < 获取当前系统时间戳 10位 秒 */
    func kCurentTimeInt() -> Int64 {
        return Int64(Date().timeIntervalSince1970 * 1000)
    }
    
    func getCategoryId(category:String) -> Int {
        switch category {
        case "AI Girlfriend":
            return 1
        case "Anime":
            return 2
        case "Game":
            return 3
        case "Celebrity":
            return 4
        case "AI Boyfriend":
            return 5
        default:
            return 0
        }
    }

    enum CodingKeys: String, CodingTableKey {
        typealias Root = RoleModel
        static let objectRelationalMapping = TableBinding(CodingKeys.self)

        case id
        case roleType
        case questionTemplate
        case icon
        case greeting
        case desc
        case lastTime
        case serverId
        case name
        case category
        case images
        case imageIndex
        case categoryId
//        case extraType

        static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
            return [id: ColumnConstraintBinding(isPrimary: true)]
        }
    }
}

// class RoleRespondModel {
//    var type: String = ""
//    var roles: [RoleModel] = []
//
//   func createWithJsonData(_ jsonData: JSON) -> RoleRespondModel{
//    type = jsonData["type"].stringValue
//    roles = jsonData["roles"].map({ _, data in
//      return RoleModel.init(data)
//    })
//       return self
//   }
//
//    class func getRecentChat() ->RoleRespondModel{
//        let resentChat = RoleRespondModel()
////        resentChat.type = "recent_chat".stringWithKey //testJJ
//        resentChat.roles = DYRoleDataManager.share.getAllModel()
//        return resentChat
//    }
//
//    class func getDefaultChat() ->RoleRespondModel{
//        let resentChat = RoleRespondModel()
//        resentChat.type = "Default"
//        resentChat.roles = DYRoleDataManager.share.getDefaultRoles()
//        return resentChat
//    }
//
//    class func getNormals() ->[RoleRespondModel]{
//        return [RoleRespondModel.getRecentChat(),RoleRespondModel.getDefaultChat()]
//    }
// }
